package com.chen.blog.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.chen.blog.common.api.CommonResult;
import com.chen.blog.common.constant.MessageConstant;
import com.chen.blog.model.Collect;
import com.chen.blog.service.CollectService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author chen
 * @since 2022-01-24
 */
@Api(tags = "收藏管理 CollectController")
@RestController
@RequestMapping("/collect")
public class CollectController {

    @Autowired
    private CollectService collectService;

    @ApiOperation("新增收藏")
    @PostMapping
    public CommonResult addCollect(@RequestBody Collect collect) {
        if (collectService.save(collect)) {
            return CommonResult.success(MessageConstant.COLLECT_BLOG_SUCCESS);
        }
        return CommonResult.error(MessageConstant.FAIL);
    }

    @ApiOperation("删除收藏")
    @DeleteMapping("/{blogId}/{userId}")
    public CommonResult deleteCollect(@PathVariable("blogId") Integer blogId, @PathVariable("userId") Integer userId) {
        if (collectService.remove(new QueryWrapper<Collect>().eq("blog_id", blogId).eq("user_id", userId))) {
            return CommonResult.success(MessageConstant.COLLECT_BLOG_FAIL);
        }
        return CommonResult.error(MessageConstant.FAIL);
    }

    @ApiOperation("检索指定id用户的收藏列表")
    @GetMapping("/{id}/{currentPage}/{pageSize}")
    public CommonResult getCollect(@PathVariable("id") Integer id, @PathVariable(value = "currentPage") Integer currentPage,
                                   @PathVariable(value = "pageSize") Integer pageSize, @RequestParam Map<String, Object> map) {
        return collectService.getCollect(id, currentPage, pageSize, map);
    }
}
